package com.zretc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 1- 拦截器的定义
 * 
 * @author Administrator
 *
 */
public class MyInterceptor implements HandlerInterceptor {

	public MyInterceptor() {
		System.out.println("**********MyInterceptor***********");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		System.out.println("**********preHandle***********" + handler);

		// preHandle访问控制做一些设置;例如： 得到session内容，判断用户是否登录
		if (request.getSession().getAttribute("user") == null) {
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return false;
		}

		return true;

		// return HandlerInterceptor.super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		System.out.println("**********postHandle*********** handler:" + handler + ", modelAndView: " + modelAndView);
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("**********afterCompletion*********** handler:" + handler);
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}
}
